$(function () {
    init();
    getUserInfo();
})

function init() {
    FastClick.attach(document.body);
    myTab("#tabBt", "#tabPage", getQueryString("index"));
}

function formHide() {
    $(".weui-mask").remove();
    form.css("transform", "scale(0.1)").css("opacity", "0.1");
    setTimeout(function () {
        form.remove();
    }, 300)
    close.remove();
    submit.remove();
}

function getUserInfo() {
    equipmemtService.queryPersonInformation({}, function (data) {
        if (data.success) {
            userId = data.data[0].staffId;
            userName = data.data[0].name;
            userRole = data.data[0].cRoleName;
            userRoleId = data.data[0].cRoleId;
            organizationName = data.data[0].organizationName;
            if (getQueryString("index") == "1") {
                getList2();
            } else {
                getList1();
            }
            $(".tabButton").on("click", function (e) {
                var index = $(e.target).index();
                if (index == 0) {
                    getList1();
                } else if (index == 1) {
                    getList2();
                } else {
                    getList3();
                }
            })

        } else {
            $.alert(data.errorMsg, function () {
                window.location.href = "/logout";
            });
        }
    })
}

function getList1() {
    equipmemtService.maintenanceList({}, function (data) {
        if (data.success) {
            $("#page1").html("");
            $(data.datas).each(function (index, item) {
                var row = $('<div class="row">\n' +
                    '                   <div class="type">\n' +
                    '                       <span class="fa fa-tools"> ' + item.questionTypeName + '</span>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="title">' + item.name + '</div>\n' +
                    '                           <div class="subTitle">' + item.orgName + " " + item.usePosition + '</div>\n' +
                    '                           <div class="subTitle"> ' + item.questionDescripe + '</div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right"  style="width:0px;">\n' +
                    //    '                           <div class="yy fa fa-clock"><span>预约</span></div>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="flex" name="applyImgs">\n' +
                    '                           </div>\n' +
                    '                           <div class="timeLine" id="submitRecord">\n' +
                    '                           </div>\n' +
                    '                           <div class="tips" id="tips">\n' +
                    '                           </div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right" style="width:120px;" id="menus">\n' +
                    '                           <a class="link fa fa-phone-alt" name="applayTel"  href="tel:' + item.telephone + '"> 联系报修人</a>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '               </div>');

                $("#page1").append(row);
                $(item.supplement).each(function (si, sd) {
                    if (sd.dataType == "111001") {
                        var img = $('<div class="imgBox" style="background-image: url(' + sd.fileAddress + ')"  title="' + sd.createTime + "：" + sd.fileDescribe + '">' + sd.fileDescribe + '</div>');
                        var boxs = row.find("[name='applyImgs']");
                        var box = boxs.eq(boxs.length - 1);
                        if (box.children().length == 3) {
                            box.after($("<div class=\"flex\" name=\"applyImgs\"></div>"));
                            box = box.next();
                        }
                        box.append(img);
                        row.find(".imgBox").each(function(i,dom){
                            var o=$(dom);
                            o.css("height", o.width() + "px");
                        });
                        img.on("click", function (e) {
                            var imgBox = $(e.target);
                            $(".weui-mask").remove();
                            $("body").append('<div class="weui-mask weui-mask--visible"></div>');
                            var imgShow = $("<div class='imgShow'>" + imgBox.attr("title") + "</div>");
                            var close = $("<span class='close fa fa-times'> </span>");
                            $("body").append(imgShow);
                            imgShow.css("background-image", imgBox.css("background-image"));
                            close.on("click", function () {
                                $(".weui-mask").remove();
                                imgShow.remove();
                                close.remove();
                                $("body").off("touchmove");
                                $("body").off("touchend");
                            })
                            $("body").append(imgShow);
                            $("body").append(close);
                            var box = imgShow[0];
                            var boxGesture = setGesture(box);  //得到一个对象
                            boxGesture.gesturemove = function (e) {  //双指移动
                                box.style.transform = "scale(" + e.scale + ")";//改变目标元素的大小
                            };
                        })

                    } else if (sd.dataType == "111002") {
                        var li = $('<li class="fa fa-exclamation-circle"> ' + sd.createTime.substr(5, 11) + " " + sd.fileDescribe + '</li>');
                        row.find("#tips").append(li);
                    }
                })
                $(item.submitRecord).each(function (si, sd) {
                    var record = $('<li class="far fa-clock"> ' + sd.createTime.substr(5, 11) + " " + sd.submitName + '</li>');
                    if (si == item.submitRecord.length - 1) record.addClass("now");
                    row.find("#submitRecord").append(record);
                    if (sd.submitType == '112003') {
                        row.find("#menus [name='repairMan']").remove();
                        row.find("#menus").append(" <a class=\"link fa fa-phone-alt\"  href=\"tel:" + sd.telephone + "\" name='repairMan'> 联系维修人</a>");
                        if (sd.toStaffId == userId) {
                            row.find("#menus [name='repairResult']").remove();
                            var result = $(" <a class=\"link fa fa-tools\"  name='repairResult'> 完成修理</a>");
                            row.find("#menus").append(result);
                            result.on("click", function () {
                                window.location.href="eresult/"+item.flowCode;
                            })
                        }
                    }
                });

                //如果此人就是申请人，那么可以进行补充说明
                if (userId == item.applyStaffId) {
                    var menu1 = $(" <a class=\"link fa fa-comment-dots\" > 补充说明</a>");
                    row.find("#menus").append(menu1);
                    row.find("#menus [name='applayTel']").remove();//联系报修人去掉
                    menu1.on("click", function () {
                        $(".weui-mask").remove();
                        $("body").append('<div class="weui-mask weui-mask--visible"></div>');
                        form = $("<div class='form TA' id='" + item.id + "'></div>");
                        form.append("<div class='flex'><div class='label'>补充信息</div></div><div class='flex'><div class='value'><textarea id='einfo' placeholder='请输入补充内容描述'></textarea></div></div>");
                        var imgUpload = $("<div class='flex'><div class='label'>上传图片（请上传相关照片）</div></div><div class='flex'><div class='value'>" +
                            "<div class='imgUpload weui-uploader__input-box'><input type='file' id='newImg' class='weui-uploader__input' accept='image/*'/></div>" +
                            "</div></div>");
                        form.append(imgUpload);
                        imgUpload.find("#newImg").on("change", function () {
                            var showImg = $("<img  id=\"eImg\" style=\"float:left;margin:10px;width:calc(33.3% - 20px);\" />");
                            imgUpload.find("#newImg").parent().before(showImg)
                            var file = imgUpload.find("#newImg")[0].files;
                            imgZip(file, showImg, 800, 800);
                            if (imgUpload.find("#newImg").parent().parent().children().length == 4) {
                                imgUpload.find("#newImg").parent().hide();
                            }
                            showImg.on("click", function () {
                                $.confirm("是否删除图片重新上传", "删除", function () {
                                    showImg.remove();
                                    imgUpload.find("#newImg").parent().show();
                                })
                            })
                        });
                        close = $("<span class='close fa fa-times'> </span>");
                        close.on("click", function () {
                            formHide();
                        }).css("left", "20px");
                        submit = $("<span class='submit fa fa-check'> 确认</span>");
                        submit.on("click", function () {
                            $.confirm("是否确认提交？", '确认', function () {

                                var param = {};
                                param.id = item.id;
                                param.fileDescribe = form.find("#einfo").val();
                                var repairSupplement = imgUpload.find("img");
                                param.repairSupplement = [];
                                for (var i = 0; i < repairSupplement.length; i++) {
                                    param.repairSupplement.push(repairSupplement[i].src);
                                }
                                equipmemtService.applyRepairSupplement(param, function (data) {
                                    if (data.success) {
                                        formHide();
                                        getList1();
                                    } else {
                                        $.alert(data.errorMsg);
                                    }
                                });

                            })
                        }).css("left", "130px").css("right", "20px");
                        $("body").append(form);
                        $("body").append(close);
                        $("body").append(submit);
                        form.find("textarea").focus();
                    })

                    var menu2 = $(" <a class=\"link fa fa-trash-alt\" > 取消报修</a>");
                    row.find("#menus").append(menu2);
                    menu2.on("click", function () {
                        $.confirm("是否确认取消申请？", '确认', function () {

                            var param = {};
                            param.state = "201801";
                            param.equipmentId = item.equipmentId;
                            param.id = item.id;
                            equipmemtService.updateRepairState(param, function (data) {
                                if (data.success) {
                                    getList1();
                                } else {
                                    $.alert(data.errorMsg);
                                }
                            });

                        })
                    })
                }


            })

        } else {
            $.alert(data.errorMsg);
        }
    })
}

function getList2() {
    equipmemtService.acceptedList({}, function (data) {
        if (data.success) {
            $("#page2").html("");
            $(data.datas).each(function (index, item) {
                var row = $('<div class="row">\n' +
                    '                   <div class="type">\n' +
                    '                       <span class="fa fa-tools"> ' + item.questionTypeName + '</span>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="title">' + item.name + '</div>\n' +
                    '                           <div class="subTitle">' + item.orgName + " " + item.usePosition + '</div>\n' +
                    '                           <div class="subTitle"> ' + item.questionDescripe + '</div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right"  style="width:0px;">\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="flex" name="applyImgs">\n' +
                    '                           </div>\n' +
                    '                           <div class="timeLine" id="submitRecord">\n' +
                    '                           </div>\n' +
                    '                           <div class="tips" id="tips">\n' +
                    '                           </div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right" style="width:120px;" id="menus">\n' +
                    '                           <a class="link fa fa-phone-alt" name="applayTel"  href="tel:' + item.telephone + '"> 联系报修人</a>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '               </div>');

                $("#page2").append(row);
                $(item.supplement).each(function (si, sd) {
                    if (sd.dataType == "111001") {
                        var img = $('<div class="imgBox" style="background-image: url(' + sd.fileAddress + ')"  title="' + sd.createTime + "：" + sd.fileDescribe + '">' + sd.fileDescribe + '</div>');
                        var boxs = row.find("[name='applyImgs']");
                        var box = boxs.eq(boxs.length - 1);
                        if (box.children().length == 3) {
                            box.after($("<div class=\"flex\" name=\"applyImgs\"></div>"));
                            box = box.next();
                        }
                        box.append(img);
                        row.find(".imgBox").each(function(i,dom){
                            var o=$(dom);
                            o.css("height", o.width() + "px");
                        });
                        img.on("click", function (e) {
                            var imgBox = $(e.target);
                            $(".weui-mask").remove();
                            $("body").append('<div class="weui-mask weui-mask--visible"></div>');
                            var imgShow = $("<div class='imgShow'>" + imgBox.attr("title") + "</div>");
                            var close = $("<span class='close fa fa-times'> </span>");
                            $("body").append(imgShow);
                            imgShow.css("background-image", imgBox.css("background-image"));
                            close.on("click", function () {
                                $(".weui-mask").remove();
                                imgShow.remove();
                                close.remove();
                                $("body").off("touchmove");
                                $("body").off("touchend");
                            })
                            $("body").append(imgShow);
                            $("body").append(close);
                            var box = imgShow[0];
                            var boxGesture = setGesture(box);  //得到一个对象
                            boxGesture.gesturemove = function (e) {  //双指移动
                                box.style.transform = "scale(" + e.scale + ")";//改变目标元素的大小
                            };
                        })

                    } else if (sd.dataType == "111002") {
                        var li = $('<li class="fa fa-exclamation-circle"> ' + sd.createTime.substr(5, 11) + " " + sd.fileDescribe + '</li>');
                        row.find("#tips").append(li);
                    }
                })
                $(item.submitRecord).each(function (si, sd) {
                    var record = $('<li class="far fa-clock"> ' + sd.createTime.substr(5, 11) + " " + sd.submitName + '</li>');
                    if (si == item.submitRecord.length - 1) record.addClass("now");
                    row.find("#submitRecord").append(record);
                    if (sd.submitType == '112003') {
                        row.find("#menus [name='repairMan']").remove();
                        row.find("#menus").append(" <a class=\"link fa fa-phone-alt\"  href=\"tel:" + sd.telephone + "\" name='repairMan'> 联系维修人</a>");
                        if (sd.toStaffId == userId) {
                            if (sd.state == "209002") {
                                row.find("#menus [name='repairResult']").remove();
                                var result = $(" <a class=\"link fa fa-tools\"  name='repairResult'> 完成修理</a>");
                                row.find("#menus").append(result);
                                result.on("click", function () {

                                })
                            } else {
                                row.find("#menus [name='repairMan']").remove();
                                row.find("#menus [name='repairResult']").remove();
                                var result = $(" <a class=\"link fa fa-tools\"  name='repairResult'> 接单受理</a>");
                                row.find("#menus").append(result);
                                result.on("click", function () {
                                    $.modal({
                                        title: "确认受理",
                                        text: "是否受理此业务？",
                                        buttons: [
                                            {
                                                text: "接受派单", onClick: function () {
                                                equipmemtService.updateRepairWorkState({
                                                    id:sd.id,
                                                    state:"209002",
                                                    repairApplyId:item.id,
                                                    repairApplyState:"201501"
                                                }, function (data) {
                                                    if (data.success) {
                                                        getList1();
                                                        getList2();
                                                    } else {
                                                        $.alert(data.errorMsg);
                                                    }
                                                });
                                            }
                                            },
                                            {
                                                text: "拒绝接单", className: "bt_warn", onClick: function () {
                                                $.confirm("确认拒绝？", "确认", function () {
                                                    equipmemtService.updateRepairWorkState({
                                                        id:sd.id,
                                                        state:"209003",
                                                        repairApplyId:item.id,
                                                        repairApplyState:"201502"
                                                    }, function (data) {
                                                        if (data.success) {
                                                            getList2();
                                                        } else {
                                                            $.alert(data.errorMsg);
                                                        }
                                                    });
                                                })
                                            }
                                            },
                                            {text: "取消操作", className: "default"},
                                        ]
                                    });
                                })
                            }
                        }
                    }
                });

                //如果此人是设备管理员 那么进行审核
                if (userRoleId == "3") {
                    var menu1 = $(" <a class=\"link fa fa-check-circle\" > 审核</a>");
                    row.find("#menus").append(menu1);
                    menu1.on("click", function () {
                        $(".weui-mask").remove();
                        $("body").append('<div class="weui-mask weui-mask--visible"></div>');
                        form = $("<div class='form TA' id='" + item.id + "'></div>");
                        form.append("<div class='flex'><div class='label'>审核结果</div><div class='value'><input id='etype' placeholder='请选择'/></div></div>");
                        form.append("<div class='flex' id='reason' style='display: none;'><div class='label'>维修描述</div></div><div class='flex'><div class='value'><textarea placeholder='请输入' id='describe'></textarea></div></div>");

                        form.find("#etype").picker({
                            title: "选择审核结果",
                            cols: [
                                {
                                    textAlign: 'center',
                                    values: ["1-公司内部维修", "2-提交维修中心", "3-驳回申请"]
                                }
                            ]
                        }).on("change", function () {
                            var value = form.find("#etype").val();
                            form.find("#reason").hide();
                            if (value.startsWith("1")) {
                                form.find("#reason").show();
                                form.find("#describe").attr("placeholder", "请输入内部维修具体方式");
                            } else if (value.startsWith("2")) {
                                form.find("#reason").show();
                                form.find("#describe").attr("placeholder", "请输入推荐中心维修的具体方式");
                            } else {
                                form.find("#reason").show();
                                form.find("#describe").attr("placeholder", "请输入驳回理由");
                            }
                        })

                        close = $("<span class='close fa fa-times'> </span>");
                        close.on("click", function () {
                            formHide();
                        }).css("left", "20px");
                        submit = $("<span class='submit fa fa-check'> 提交审核</span>");
                        submit.on("click", function () {
                            $.confirm("是否确认提交审核意见？", '确认', function () {

                                var param = {};
                                param.type = form.find("#etype").val();
                                param.questionDescripe = item.questionDescripe + "【" + userName + "审核意见:" + form.find("#describe").val() + "】";
                                param.equipmentId = item.equipmentId;
                                param.id = item.id;
                                param.staffName = item.applyName;
                                param.telephone = item.telephone;
                                equipmemtService.checkRepair(param, function (data) {
                                    if (data.success) {
                                        formHide();
                                        getList2();
                                    } else {
                                        $.alert(data.errorMsg);
                                    }
                                });

                            })
                        }).css("left", "130px").css("right", "20px");
                        $("body").append(form);
                        $("body").append(close);
                        $("body").append(submit);
                        form.find("textarea").focus();
                    })

                }
            })

        } else {
            $.alert(data.errorMsg);
        }
    })
}

function getList3() {
    equipmemtService.historyList({}, function (data) {
        $("#page3").html("");
        if (data.success) {
            $(data.datas).each(function (index, item) {
                var row = $('<div class="row">\n' +
                    '                   <div class="type">\n' +
                    '                       <span class="fa fa-tools"> ' + item.questionTypeName + '</span>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="title">' + item.name + '</div>\n' +
                    '                           <div class="subTitle">' + item.orgName + " " + item.usePosition + '</div>\n' +
                    '                           <div class="subTitle"> ' + item.questionDescripe + '</div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right"  style="width:0px;">\n' +
                    //    '                           <div class="yy fa fa-clock"><span>预约</span></div>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="flex" name="applyImgs">\n' +
                    '                           </div>\n' +
                    '                           <div class="timeLine" id="submitRecord">\n' +
                    '                           </div>\n' +
                    '                           <div class="tips" id="tips">\n' +
                    '                           </div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right" style="width:120px;" id="menus">\n' +
                    '                           <a class="link fa fa-phone-alt" name="applayTel"  href="tel:' + item.telephone + '"> 联系报修人</a>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '               </div>');

                $("#page3").append(row);
                $(item.supplement).each(function (si, sd) {
                    if (sd.dataType == "111001") {
                        var img = $('<div class="imgBox" style="background-image: url(' + sd.fileAddress + ')"  title="' + sd.createTime + "：" + sd.fileDescribe + '">' + sd.fileDescribe + '</div>');
                        var boxs = row.find("[name='applyImgs']");
                        var box = boxs.eq(boxs.length - 1);
                        if (box.children().length == 3) {
                            box.after($("<div class=\"flex\" name=\"applyImgs\"></div>"));
                            box = box.next();
                        }
                        box.append(img);
                        row.find(".imgBox").each(function(i,dom){
                            var o=$(dom);
                            o.css("height", o.width() + "px");
                        });
                        img.on("click", function (e) {
                            var imgBox = $(e.target);
                            $(".weui-mask").remove();
                            $("body").append('<div class="weui-mask weui-mask--visible"></div>');
                            var imgShow = $("<div class='imgShow'>" + imgBox.attr("title") + "</div>");
                            var close = $("<span class='close fa fa-times'> </span>");
                            $("body").append(imgShow);
                            imgShow.css("background-image", imgBox.css("background-image"));
                            close.on("click", function () {
                                $(".weui-mask").remove();
                                imgShow.remove();
                                close.remove();
                                $("body").off("touchmove");
                                $("body").off("touchend");
                            })
                            $("body").append(imgShow);
                            $("body").append(close);
                            var box = imgShow[0];
                            var boxGesture = setGesture(box);  //得到一个对象
                            boxGesture.gesturemove = function (e) {  //双指移动
                                box.style.transform = "scale(" + e.scale + ")";//改变目标元素的大小
                            };
                        })

                    } else if (sd.dataType == "111002") {
                        var li = $('<li class="fa fa-exclamation-circle"> ' + sd.createTime.substr(5, 11) + " " + sd.fileDescribe + '</li>');
                        row.find("#tips").append(li);
                    }
                })
                $(item.submitRecord).each(function (si, sd) {
                    var record = $('<li class="far fa-clock"> ' + sd.createTime.substr(5, 11) + " " + sd.submitName + '</li>');
                    if (si == item.submitRecord.length - 1) record.addClass("now");
                    row.find("#submitRecord").append(record);
                    if (sd.submitType == '112003') {
                        row.find("#menus [name='repairMan']").remove();
                        row.find("#menus").append(" <a class=\"link fa fa-phone-alt\"  href=\"tel:" + sd.telephone + "\" name='repairMan'> 联系维修人</a>");

                    }
                });

                //如果此人就是申请人，那么可以进行补充说明
                if (userId == item.applyStaffId) {

                    var menu2 = $(" <a class=\"link fa fa-trash-alt\" > 删除记录</a>");
                    row.find("#menus").append(menu2);
                    menu2.on("click", function () {
                        $.confirm("是否确认删除记录？", '确认', function () {
                            var param = {};
                            param.validFlag = "100002";
                            param.equipmentId = item.equipmentId;
                            param.id = item.id;
                            equipmemtService.updateRepairState(param, function (data) {
                                if (data.success) {
                                    getList3();
                                } else {
                                    $.alert(data.errorMsg);
                                }
                            });

                        })
                    })
                }
            })

        } else {
            $.alert(data.errorMsg);
        }
    })
}